#include "PopupUtil.h"
//#include "NavigatorUtil.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Base::Runtime;
using namespace Osp::Media;
using namespace Osp::Graphics;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Graphics;


PopupUtil::PopupUtil(void)
:animationFrameList (NULL),
 __listener			(NULL)
{

}

PopupUtil::~PopupUtil(void)
{
	AppLog(" PopupUtil deleted");
	//RemoveAllControls();
	if(animationFrameList)
	{
		animationFrameList->RemoveAll(true);
		delete animationFrameList;
		animationFrameList=NULL;
	}
	AppLog(" finish deleted PopupUtil");
}
void
PopupUtil::Initialize(){
	Popup::Construct(String(L"IDP_POPUP_WAIT"));
	__pLbText = static_cast<Label *>(GetControl(String(L"IDC_LABEL1")));
}
void
PopupUtil::Initialize(popupStyle style, typeSearch type,IPopupListener &listener)
{
	__listener=&listener;
	__type=type;
	if (style==COMMON){
		InitCommon();
		return;
	}
	if(animationFrameList==NULL){
		animationFrameList=new ArrayList();
		animationFrameList->Construct();
	}
	Bitmap *pBitmap1 = GetBitmapN("/blue/120x120/progressing00_big.png");
	Bitmap *pBitmap2 = GetBitmapN("/blue/120x120/progressing01_big.png");
	Bitmap *pBitmap3 = GetBitmapN("/blue/120x120/progressing02_big.png");
	Bitmap *pBitmap4 = GetBitmapN("/blue/120x120/progressing03_big.png");
	Bitmap *pBitmap5 = GetBitmapN("/blue/120x120/progressing04_big.png");
	Bitmap *pBitmap6 = GetBitmapN("/blue/120x120/progressing05_big.png");
	Bitmap *pBitmap7 = GetBitmapN("/blue/120x120/progressing06_big.png");
	Bitmap *pBitmap8 = GetBitmapN("/blue/120x120/progressing07_big.png");
		// Create AnimationFrames
	long duration = 1000 / 8;
	AnimationFrame *pAniFrame1 = new AnimationFrame(*pBitmap1, duration);
	AnimationFrame *pAniFrame2 = new AnimationFrame(*pBitmap2, duration);
	AnimationFrame *pAniFrame3 = new AnimationFrame(*pBitmap3, duration);
	AnimationFrame *pAniFrame4 = new AnimationFrame(*pBitmap4, duration);
	AnimationFrame *pAniFrame5 = new AnimationFrame(*pBitmap5, duration);
	AnimationFrame *pAniFrame6 = new AnimationFrame(*pBitmap6, duration);
	AnimationFrame *pAniFrame7 = new AnimationFrame(*pBitmap7, duration);
	AnimationFrame *pAniFrame8 = new AnimationFrame(*pBitmap8, duration);

	//		animationFrameList;
	animationFrameList->RemoveAll(true);
	animationFrameList->Add(*pAniFrame1);
	animationFrameList->Add(*pAniFrame2);
	animationFrameList->Add(*pAniFrame3);
	animationFrameList->Add(*pAniFrame4);
	animationFrameList->Add(*pAniFrame5);
	animationFrameList->Add(*pAniFrame6);
	animationFrameList->Add(*pAniFrame7);
	animationFrameList->Add(*pAniFrame8);

	delete pBitmap1;delete pBitmap2;delete pBitmap3;delete pBitmap4;delete pBitmap5;delete pBitmap6;delete pBitmap7;delete pBitmap8;

	if(style==WAITING_WITHOUT_PROGRESS){
		InitWait();
	}else if(style==WAITING_PROGRESS){
		InitWaitProgress();
	}
}

void
PopupUtil::SetMessage(const Osp::Base::String& text){
	__pLbText->SetText(text);
}
void
PopupUtil::SetProgressStatus(int value){
	__pProgress->SetValue(value);
}
void
PopupUtil::ShowPopup(){
	SetShowState(true);
	Show();
}

void
PopupUtil::OnActionPerformed(const Osp::Ui::Control& source, int actionId){

	if(__listener) __listener->OnUserAction(actionId, __type);

}

void
PopupUtil::InitWait(){

	Popup::Construct(String(L"IDP_POPUP_WAITING"));
	Button* pButton = static_cast<Button*>(GetControl(String(L"IDC_BUTTON1")));
	pButton->SetActionId(ID_ACTION_RESPONSE);
	pButton->AddActionEventListener(*this);
	//__pLbText = static_cast<Label *>(GetControl(String(L"IDC_LABEL1")));
	AppLog("sdsds");
	//Animation bat buoc phai dung kieu con tro
	Animation* pAnimation = new Animation();
	pAnimation->Construct(Rectangle(121,26,120,120), *animationFrameList);
	pAnimation->SetRepeatCount(10000);
	pAnimation->AddAnimationEventListener(*this);
	AddControl(*pAnimation);
	pAnimation->Play();
}
void
PopupUtil::InitWaitProgress(){

	Popup::Construct(String(L"IDP_POPUP_PROGRESS_WAITING"));
	Button* pButton = static_cast<Button*>(GetControl(String(L"IDC_BUTTON1")));
	pButton->SetActionId(ID_ACTION_RESPONSE);
	pButton->AddActionEventListener(*this);
	//__pLbText = static_cast<Label *>(GetControl(String(L"IDC_LABEL1")));
	__pProgress=static_cast<Progress*>(GetControl(String(L"IDC_PROGRESS1")));
	if (__pProgress!=null){
		__pProgress->SetValue(10);
	}
	//Animation bat buoc phai dung kieu con tro
	Animation* pAnimation = new Animation();
	pAnimation->Construct(Rectangle(126,20,120,120), *animationFrameList);
	pAnimation->SetRepeatCount(10000);
	pAnimation->AddAnimationEventListener(*this);
	AddControl(*pAnimation);
	pAnimation->Play();

}
void
PopupUtil::InitCommon(){

	Popup::Construct(String(L"IDP_POPUPOK"));
	Button* pButton = static_cast<Button*>(GetControl(String(L"IDC_BUTTON1")));
	pButton->SetActionId(ID_ACTION_RESPONSE);
	pButton->AddActionEventListener(*this);
	__pLbText = static_cast<Label *>(GetControl(String(L"IDC_LABEL1")));

}
void PopupUtil::SetText(const Osp::Base::String& info){
	__pLbText->SetText(info);
}
void
PopupUtil::ShowMessageBox(Osp::Base::String title, Osp::Base::String text)
{
	int modalResult = 0;
	MessageBox myMSGBox;
	myMSGBox.Construct(title, text, MSGBOX_STYLE_OK);
	myMSGBox.ShowAndWait(modalResult);
}

int
PopupUtil::ShowYesNoMessageBox(Osp::Base::String title, Osp::Base::String text)
{
	int modalResult = 0;
	MessageBox myMSGBox;
	myMSGBox.Construct(title, text, MSGBOX_STYLE_YESNO);
	myMSGBox.ShowAndWait(modalResult);

	return modalResult;
}
//result
//PopupUtil::ShowPopup(int millisec)
//{
//	result r = E_SUCCESS;
//	r = Show();
//	AppLog("Sleep");
//	Thread::Sleep(millisec);
//	CloseWaitingPopup();
//	return r;
//}
//void
//PopupUtil::CloseWaitingPopup(void)
//{
//	if (__pWaitingPopup)
//	{
//		delete __pWaitingPopup;
//		__pWaitingPopup = null;
//	}
//}
Osp::Graphics::Bitmap*
PopupUtil::GetBitmapN(const Osp::Base::String& name)
{
	Bitmap* pBitmap = null;
	String fullname = L"/Res";
	fullname.Append(name);
	Image*  pImage = new Image();
	pImage->Construct();
	if(fullname.EndsWith(L"jpg"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"bmp"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"png"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
	}
	else if (fullname.EndsWith(L"gif"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	delete pImage;
	return pBitmap;
}
